﻿using UnityEngine;
using System.Collections;
using COC.Character;
using COC.CComponent;

public class DraggableCameraComponent : BaseComponent
{
    bool draggable = true;
    int middleWidth = Screen.width / 2;
    int middleHeight = Screen.height / 2;
    private Vector3 dragOrigin;
    private float dragSpeed = 2f;
    public Transform target;
    Vector3 pos2=Vector3.zero;
    GameObject go;
    private Vector3 screenSpace;
    private Vector3 offset;

    public override void OnStart()
    {
        go = GameObject.CreatePrimitive(PrimitiveType.Cube);
        go.transform.localScale = new Vector3(10, 10, 10);

        go.renderer.renderer.material.color = Color.green;
        go.SetActive(false);
    }

    public override void OnAwake()
    {
        base.OnAwake();
    }

    /// <summary>
    /// Drags the window.通过鼠标拖拽camera
    /// </summary>
    void DragWindowByMouse()
    {


        if (Input.GetMouseButtonDown(0))
        {
            dragOrigin = Input.mousePosition;



            computeMouseCusor();
            Debug.Log("dragOrigin:" + dragOrigin.x);

            return;
        }
        if (!Input.GetMouseButton(0))
        {
            // go.SetActive(false);
            return;
        }
        go.transform.localPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z)) + offset;


        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
        Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);

        Debug.Log("move:" + move.x);
        target.Translate(move, Space.World);

    }

    private void computeMouseCusor()
    {
        screenSpace = Camera.main.WorldToScreenPoint(dragOrigin);

        offset = dragOrigin -
                 Camera.main.WorldToScreenPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        pos2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        go.transform.localPosition = new Vector3(pos2.x, 7f, pos2.z);
        //go.SetActive(true);
    }

    void Update()
    {

        DragWindowByMouse();


    }

    void OnGUI()
    {
        //GUI.Label (new Rect (10, 25, 100, 25), "x:" + Input.mousePosition.x + " y:" + Input.mousePosition.y);
        //GUI.Label (new Rect (middleWidth, middleHeight, 101, 102), Input.multiTouchEnabled + "");
    }


}
